package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.*;
import com.qf.mapper.PositionMapper;
import com.qf.service.PositionService;
import com.qf.utils.NumberUtil;
import com.sun.org.apache.xpath.internal.operations.Number;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/24
 * 郭程鹏
 * gcp1909
 * 面向代码面向君,不负时光不负卿
 */
@Service
public class PositionImpl implements PositionService {

    //职位设置
    @Autowired
    private PositionMapper positionMapper;

    @Override
    public PageInfo<UserRole> allPosition(Integer page, Integer size, Map<String,String> map) {

        //使用分页助手
        PageHelper.startPage(page,size);

        //得到所有的职位信息
        List<UserRole> userRoles = positionMapper.allPosition(map);

        PageInfo<UserRole> rolePageInfo = new PageInfo<>(userRoles);

        return rolePageInfo;
    }

    @Override
    public UserRole roleDesc(Integer roleId) {

        UserRole userRole = positionMapper.roleDesc(roleId);

        return userRole;
    }

    //添加角色的前置工作
    @Override
    public List<SystemMnue> getMnues() {
        List<SystemMnue> mnues = positionMapper.getMnues();
        //一级菜单
        ArrayList fristMnues = new ArrayList<>();
        for (SystemMnue mnue : mnues) {

            if (mnue.getSuperMnueId()==0){
                //二级菜单
                ArrayList secondMnues=new ArrayList<>();

                for (SystemMnue smnue : mnues) {
                    //菜单中有上级菜单且相等
                    if (mnue.getMnueId()==smnue.getSuperMnueId()){
                        //保存到二级菜单中
                        secondMnues.add(smnue);
                    }
                }

                //封装一个完整的一二级菜单
                mnue.setSecondMnue(secondMnues);
                //将一个完整的二级菜单存放到一级菜单中
                fristMnues.add(mnue);
            }


        }
        return fristMnues;
    }

    @Override
    public List<PositionClass> getPosition() {
        List<PositionClass> position = positionMapper.getPosition();
        return position;
    }

    @Override
    public List<Department> getAllDept() {
        List<Department> allDept = positionMapper.getAllDept();
        return allDept;
    }

    @Override
    public String creatUserId() {
        String number = NumberUtil.getNumber(4);

        return number;
    }


    //添加角色的前置查询

    public Map<Object,Object> preAdd(){
        List<SystemMnue> mnues = getMnues();
        List<PositionClass> position = getPosition();
        List<Department> allDept = getAllDept();
        String roleId = creatUserId();
        HashMap<Object, Object> preAddMap = new HashMap<>();
        preAddMap.put("mnues",mnues);
        preAddMap.put("position",position);
        preAddMap.put("allDept",allDept);
        preAddMap.put("roleId",roleId);
        return  preAddMap;
    }

    @Transactional
    @Override
    public void addToRole(UserRole userRole,List<Integer> mnueId,UserPosition userPosition) {

        positionMapper.addToUserPosition(userPosition);
        userRole.setUserPosId(userPosition.getId());
        positionMapper.addToRole(userRole);
        positionMapper.addMiddle(userRole.getRoleId(),mnueId);

    }

    //删除指定角色
    @Override
    @Transactional
    public int deleteRole(String roleId) {
        int i = positionMapper.deleteRole(roleId);
        return i;
    }

    //批量删除角色
    @Override
    public int deleteAllRole(List<Integer> roleId) {
        int role = positionMapper.deleteAllRole(roleId);
        return role;
    }


}
